/**
 * Moondance UML modeling tool
 * 
 * @author Dascalu Laurentiu (dascalu.laurentziu@gmail.com).
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.xmi.converter;

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import ro.pub.cs.se.moondance.MoondanceData;
import ro.pub.cs.se.moondance.model.UMLClassDiagram;
import ro.pub.cs.se.moondance.model.UMLDiagram;
import ro.pub.cs.se.moondance.model.UMLStateDiagram;

public class XMIConverter {
	private String fileName;

	public XMIConverter() {

	}

	/**
	 * @param Output
	 *            stream
	 */
	public byte[] convert(String fileName) {
		try {
			this.fileName = fileName;
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document document = db.newDocument();

			startAdding(document);

			TransformerFactory transfac = TransformerFactory.newInstance();
			Transformer trans = transfac.newTransformer();
			trans.setOutputProperty(OutputKeys.INDENT, "yes");

			DOMSource source = new DOMSource(document);
			StringWriter sw = new StringWriter();
			StreamResult result = new StreamResult(sw);

			trans.transform(source, result);

			byte[] b = new byte[sw.toString().length()];

			for (int i = 0; i < sw.toString().length(); i++)
				b[i] = (byte) sw.toString().charAt(i);

			return b;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	private Element addMainPacket(Document document) {
		Element rootElement = document.createElement(XMICommon.UML_PACKAGE);

		rootElement.setAttribute("xmi:version", "2.1");
		rootElement.setAttribute("xmlns:xmi",
				"http://schema.omg.org/spec/XMI/2.1");
		rootElement.setAttribute("xmlns:uml",
				"http://www.eclipse.org/uml2/3.0.0/UML");
		rootElement.setAttribute("xmi:id", MoondanceData.newRandomString());
		rootElement.setAttribute("name", fileName);

		document.appendChild(rootElement);

		return rootElement;
	}

	private void startAdding(Document document) {
		Element rootElement = addMainPacket(document);
		UMLDiagram currentDiagram = MoondanceData.getCurrentDiagram();
		IXMIUMLDiagram generator = null;

		if (currentDiagram instanceof UMLClassDiagram) {
			generator = new XMIClassDiagram();
		}
		else if (currentDiagram instanceof UMLStateDiagram) {
			generator = new XMIStateDiagram();
		}

		generator.add(rootElement, document, currentDiagram);
	}
}
